#include "cmdvip.h"

#include "../mainctrl.h"
#include "../cmd/dataprepare.h"
#include "commmsg.h"
#include "../cmd/cmdapi.h"

CT_REGISTER_ASYNCCMD(ID_MAIN_GET_VIP_INFO, CCmdGetVipInfo);

int CCmdGetVipInfo::Do(void* pData)
{
    CT_ASSERT(pData);

    //解析消息
    CCmdPBDoParam* pstParam = (CCmdPBDoParam*) pData;
    pstMainCtrl = (CMainCtrl*) pstParam->m_pMain;

    if (pstParam->m_pstMsgHead->msgid() != ID_MAIN_GET_VIP_INFO||
        pstParam->m_pstMsgHead->msgtype() != CSMsgDef::EMT_REQUEST)
    {
        CT_ERROR(("Cmd Stat Err Cmd=%d CmdID=%d Tpye=%d",
            ID_MAIN_GET_VIP_INFO,
            pstParam->m_pstMsgHead->msgid(),
            pstParam->m_pstMsgHead->msgtype()));
        return Done(RET_SYS_ERROR);
    }

    stMsgHead = *(pstParam->m_pstMsgHead);
    stEnpHead = *(pstParam->m_pstEnpHead);
    int iRet = pstMainCtrl->DecodePBMsg(stReq, pstParam);
    if (iRet)
    {
        return AnsClients(iRet);
    }
    //消息解析结束

    uiGID = stReq.gid();
    CT_TRACE(("get ReqVipInfo GID: %u", uiGID));

    //检查登录
    if (pstMainCtrl->CheckLogin(uiGID, stEnpHead)!=RET_OK)
    {
        return AnsClients(RET_NOT_LOGIN);
    }

    LogicDo(RET_OK);
    return RET_OK;
}

int CCmdGetVipInfo::LogicDo(int iRet)
{
    if (iRet != 0)
    {
        return AnsClients(iRet);
    }

    PREPARE_PLAYER_BASE_INFO(this, uiGID);
    ASSERT_PLAYER_PREPARED(uiGID);
    PREPARE_PLAYER_ONLINE_DATA(this, uiGID);

    int iGidIndex = pstMainCtrl->m_stPlayerBaseInfoHash.HashFind(uiGID);
    if (iGidIndex < 0)
    {
        CT_ERROR (("CCmdGetVipInfo info(id = %u), not exist.", uiGID));
        return AnsClients(RET_SYSTEM_FAIL);
    }
    CPlayerBaseInfo& stBaseInfo = pstMainCtrl->m_stPlayerBaseInfoHash[iGidIndex];

    if (stBaseInfo._ucIsInited != 1)
    {
        return AnsClients(ERROR_NOT_INITED);
    }

    pstMainCtrl->m_stVipMgr.GetVipInfo(stBaseInfo, stAns);

    stAns.set_gid(uiGID);
    stAns.set_ret(RET_OK);

    return AnsClients(RET_OK);
}

int CCmdGetVipInfo::AnsClients(int iRet)
{
    pstMainCtrl->m_stEncodeBuf.Clear();

    stMsgHead.set_msgtype(CSMsgDef::EMT_ANSWER);
    stMsgHead.set_msgret( iRet );

    if (iRet != RET_OK)
    {
        pstMainCtrl->ReturnMsgHead(stMsgHead, stEnpHead);
        return Done(iRet);
    }

    pstMainCtrl->EncodePBMsg(stMsgHead, pstMainCtrl->m_stEncodeBuf);
    pstMainCtrl->EncodePBMsg(stAns, pstMainCtrl->m_stEncodeBuf);

    if (pstMainCtrl->AnswerToClient(stEnpHead, pstMainCtrl->m_stEncodeBuf, true))
    {
        CT_WARNING(("vip enp package error"));
        return Done(RET_SYS_ERROR);
    }

    return Done(iRet);
}

//////////////////////////////////////////////////////////////////////////
CT_REGISTER_ASYNCCMD(ID_MAIN_VIP_GIFT_INFO, CCmdVipGiftInfo);

int CCmdVipGiftInfo::Do(void* pData)
{
    CT_ASSERT(pData);

    //解析消息
    CCmdPBDoParam* pstParam = (CCmdPBDoParam*) pData;
    pstMainCtrl = (CMainCtrl*) pstParam->m_pMain;

    if (pstParam->m_pstMsgHead->msgid() != ID_MAIN_VIP_GIFT_INFO ||
        pstParam->m_pstMsgHead->msgtype() != CSMsgDef::EMT_REQUEST)
    {
        CT_ERROR(("Cmd Stat Err Cmd=%d CmdID=%d Tpye=%d",
            ID_MAIN_VIP_GIFT_INFO,
            pstParam->m_pstMsgHead->msgid(),
            pstParam->m_pstMsgHead->msgtype()));
        return Done(RET_SYS_ERROR);
    }

    stMsgHead = *(pstParam->m_pstMsgHead);
    stEnpHead = *(pstParam->m_pstEnpHead);
    int iRet = pstMainCtrl->DecodePBMsg(stReq, pstParam);
    if (iRet)
    {
        return AnsClients(iRet);
    }
    //消息解析结束

    uiGID = stReq.gid();
    CT_TRACE(("req vip gift info:[%u]", uiGID));

    //检查登录
    if (pstMainCtrl->CheckLogin(uiGID, stEnpHead)!=RET_OK)
    {
        return AnsClients(RET_NOT_LOGIN);
    }

    LogicDo(RET_OK);
    return RET_OK;
}

int CCmdVipGiftInfo::LogicDo(int iRet)
{
    if (iRet != 0)
    {
        return AnsClients(iRet);
    }

    PREPARE_PLAYER_BASE_INFO(this, uiGID);
    ASSERT_PLAYER_PREPARED(uiGID);
    PREPARE_PLAYER_ONLINE_DATA(this, uiGID);

    int iIndex = pstMainCtrl->m_stPlayerBaseInfoHash.HashFind(uiGID);
    if (iIndex < 0)
    {
        return AnsClients(RET_SYS_ERROR);
    }
    CPlayerBaseInfo& stPlayerBaseInfo = pstMainCtrl->m_stPlayerBaseInfoHash[iIndex];

    if (stPlayerBaseInfo._ucIsInited != 1)
    {
        return AnsClients(ERROR_NOT_INITED);
    }

    int iGiftType = stReq.gift_type(); //取得礼包类型

    iRet = pstMainCtrl->m_stVipMgr.GetVipGiftInfo(stPlayerBaseInfo, iGiftType, stAns);

    //设置返回值
    stAns.set_gid( uiGID );
    stAns.set_gift_type( iGiftType );

    return AnsClients(iRet);
}

int CCmdVipGiftInfo::AnsClients(int iRet)
{
    pstMainCtrl->m_stEncodeBuf.Clear();

    stMsgHead.set_msgtype(CSMsgDef::EMT_ANSWER);
    stMsgHead.set_msgret( RET_OK );

    stAns.set_ret( iRet );

    pstMainCtrl->EncodePBMsg(stMsgHead, pstMainCtrl->m_stEncodeBuf);
    pstMainCtrl->EncodePBMsg(stAns, pstMainCtrl->m_stEncodeBuf);

    if (pstMainCtrl->AnswerToClient(stEnpHead, pstMainCtrl->m_stEncodeBuf, true))
    {
        CT_WARNING(("vip enp package error"));
        return Done(RET_SYS_ERROR);
    }

    return Done(iRet);
}

/////////////////////////////////////////////////////////////////////
CT_REGISTER_ASYNCCMD(ID_MAIN_OPEN_VIP_GIFT, CCmdVipOpenGift);

int CCmdVipOpenGift::Do(void* pData)
{
    CT_ASSERT(pData);

    //解析消息
    CCmdPBDoParam* pstParam = (CCmdPBDoParam*) pData;
    pstMainCtrl = (CMainCtrl*) pstParam->m_pMain;

    if (pstParam->m_pstMsgHead->msgid() != ID_MAIN_OPEN_VIP_GIFT||
        pstParam->m_pstMsgHead->msgtype() != CSMsgDef::EMT_REQUEST)
    {
        CT_ERROR(("Cmd Stat Err Cmd=%d CmdID=%d Tpye=%d",
            ID_MAIN_OPEN_VIP_GIFT,
            pstParam->m_pstMsgHead->msgid(),
            pstParam->m_pstMsgHead->msgtype()));
        return Done(RET_SYS_ERROR);
    }

    stMsgHead = *(pstParam->m_pstMsgHead);
    stEnpHead = *(pstParam->m_pstEnpHead);
    int iRet = pstMainCtrl->DecodePBMsg(stReq, pstParam);
    if (iRet)
    {
        return AnsClients(iRet);
    }
    //消息解析结束

    uiGID = stReq.gid();
    CT_TRACE(("vip req open gift gid:[%u]", uiGID));

    //检查登录
    if (pstMainCtrl->CheckLogin(uiGID, stEnpHead)!=RET_OK)
    {
        return AnsClients(RET_NOT_LOGIN);
    }

    LogicDo(RET_OK);
    return RET_OK;
}

int CCmdVipOpenGift::LogicDo(int iRet)
{
    if (iRet != 0)
    {
        return AnsClients(iRet);
    }

    PREPARE_PLAYER_BASE_INFO(this, uiGID);
    ASSERT_PLAYER_PREPARED(uiGID);

    PREPARE_PLAYER_DETAIL_INFO(this, uiGID);
    ASSERT_PLAYER_DETAIL_PREPARED(uiGID);

    PREPARE_UNACT_PARTNER_INFO(this, uiGID);
    ASSERT_UNACT_PARTNER_PREPARED(uiGID);

    PREPARE_PLAYER_DETAIL_EX1_INFO(this, uiGID);
    ASSERT_PLAYER_DETAIL_EX1_INFO(uiGID);

    PREPARE_PLAYER_ONLINE_DATA(this, uiGID);

    int iIndex = pstMainCtrl->m_stPlayerBaseInfoHash.HashFind(uiGID);
    if (iIndex < 0)
    {
        return AnsClients(RET_SYS_ERROR);
    }
    CPlayerBaseInfo& stPlayerBaseInfo = pstMainCtrl->m_stPlayerBaseInfoHash[iIndex];

    if (stPlayerBaseInfo._ucIsInited != 1)
    {
        return AnsClients(ERROR_NOT_INITED);
    }

    iIndex = pstMainCtrl->m_stPlayerDetailInfoHash.HashFind(uiGID);
    if (iIndex < 0)
    {
        CT_ERROR(("no detail data user(%u)", uiGID));
        return AnsClients(RET_SYSTEM_FAIL);
    }
    CPlayerDetailInfo& stPlayerDetail = pstMainCtrl->m_stPlayerDetailInfoHash[iIndex];

    iIndex = pstMainCtrl->m_stUnactPartnerInfoHash.HashFind(uiGID);
    if (iIndex < 0)
    {
        CT_ERROR(("no unact partner data user(%u)", uiGID));
        return AnsClients(RET_SYSTEM_FAIL);
    }

    iIndex = pstMainCtrl->m_stPlayerDetailEx1InfoHash.HashFind(uiGID);
    if (iIndex < 0)
    {
        CT_ERROR(("get user detail ex1 info error, uid = %u", uiGID));
        return AnsClients(RET_SYSTEM_FAIL);
    }
    CPlayerDetailInfoEx1& stPlayerDetailEx1Info =
        pstMainCtrl->m_stPlayerDetailEx1InfoHash[iIndex];

    int iGiftId = 0;
    int iGiftType = stReq.gift_type(); //取得礼包类型

    if( iGiftType==CONST_VIP_LEVEL_GIFT)//等级礼包
    {
        iGiftId = stReq.gift_id(); //取得礼包id
    }

    iRet = pstMainCtrl->m_stVipMgr.GetVipGiftOpen(stPlayerBaseInfo, stPlayerDetail, stPlayerDetailEx1Info, iGiftId, iGiftType, stAns);

    //设置返回值
    stAns.set_gid( uiGID );
    stAns.set_ret( iRet );
    stAns.set_gift_type( iGiftType );
    stAns.set_gift_id( iGiftId );

    return AnsClients(RET_OK);
}

int CCmdVipOpenGift::AnsClients(int iRet)
{
    pstMainCtrl->m_stEncodeBuf.Clear();

    stMsgHead.set_msgtype(CSMsgDef::EMT_ANSWER);
    stMsgHead.set_msgret( RET_OK );

    pstMainCtrl->EncodePBMsg(stMsgHead, pstMainCtrl->m_stEncodeBuf);
    pstMainCtrl->EncodePBMsg(stAns, pstMainCtrl->m_stEncodeBuf);

    if (pstMainCtrl->AnswerToClient(stEnpHead, pstMainCtrl->m_stEncodeBuf, true))
    {
        CT_WARNING(("vip enp package error"));
        return Done(RET_SYS_ERROR);
    }

    return Done(iRet);
}

//////////////////////////////////////////////////////////////////////////

CT_REGISTER_ASYNCCMD(ID_MAIN_YELLOW_GIFT, CCmdYellowGift);

int CCmdYellowGift::Do(void* pData)
{
    CT_ASSERT(pData);

    //解析消息
    CCmdPBDoParam* pstParam = (CCmdPBDoParam*) pData;
    pstMainCtrl = (CMainCtrl*) pstParam->m_pMain;

    if (pstParam->m_pstMsgHead->msgid() != ID_MAIN_YELLOW_GIFT||
        pstParam->m_pstMsgHead->msgtype() != CSMsgDef::EMT_REQUEST)
    {
        CT_ERROR(("Cmd Stat Err Cmd=%d CmdID=%d Tpye=%d",
            ID_MAIN_YELLOW_GIFT,
            pstParam->m_pstMsgHead->msgid(),
            pstParam->m_pstMsgHead->msgtype()));
        return Done(RET_SYS_ERROR);
    }

    stMsgHead = *(pstParam->m_pstMsgHead);
    stEnpHead = *(pstParam->m_pstEnpHead);
    int iRet = pstMainCtrl->DecodePBMsg(stReq, pstParam);
    if (iRet)
    {
        return AnsClients(iRet);
    }
    //消息解析结束

    uiGID = stReq.gid();

    //检查登录
    if (pstMainCtrl->CheckLogin(uiGID, stEnpHead)!=RET_OK)
    {
        return AnsClients(RET_NOT_LOGIN);
    }

    bHasGetVipInfo = false;
    bIsGettingYellowInfo = false;
    bIsGettingBlueInfo = false;
    bIsGetting3366Info = false;

    bHasGetYellowInfo = false;
    bHasGetBlueInfo = false;
    bHasGet3366Info = false;

    LogicDo(RET_OK);
    return RET_OK;
}

int CCmdYellowGift::LogicDo(int iRet)
{
    if (iRet != 0)
    {
        return AnsClients(iRet);
    }

    PREPARE_PLAYER_BASE_INFO(this, uiGID);
    PREPARE_PLAYER_ONLINE_DATA(this, uiGID);

    int iIndex = pstMainCtrl->m_stPlayerBaseInfoHash.HashFind(uiGID);
    if (iIndex < 0)
    {
        return AnsClients(RET_SYS_ERROR);
    }
    CPlayerBaseInfo& stPlayerBaseInfo = pstMainCtrl->m_stPlayerBaseInfoHash[iIndex];

    if (stPlayerBaseInfo._ucIsInited != 1)
    {
        return AnsClients(ERROR_NOT_INITED);
    }
    CYellowGiftData& stYellowGiftData = stPlayerBaseInfo._stYellowGiftData;
    CBlueGiftData& stBlueGiftData = stPlayerBaseInfo._stBlueGiftData;

    iIndex = pstMainCtrl->m_stPlayerOnlineDataHash.HashFind(uiGID);
    if (iIndex < 0)
    {
        return AnsClients(RET_SYS_ERROR);
    }
    CPlayerOnlineData& stPlayerOnlineData = pstMainCtrl->m_stPlayerOnlineDataHash[iIndex];

    //获取最新的VIP信息
    bool bIsFromYellow = false;
    bool bIsFromBlue = false;
    bool bIsFrom3366 = false;

    if (stPlayerOnlineData._bIsTencentLogin)
    {
        if (TENCENT_LOGIN_FROM_PENGYOU == stPlayerOnlineData._ucTencentLoginFrom
            || TENCENT_LOGIN_FROM_QZONE == stPlayerOnlineData._ucTencentLoginFrom)
        {
            bIsFromYellow = true;
        }
        else if (TENCENT_LOGIN_FROM_QQGAME == stPlayerOnlineData._ucTencentLoginFrom)
        {
            bIsFromBlue = true;
        }
        else if (TENCENT_LOGIN_FROM_3366 == stPlayerOnlineData._ucTencentLoginFrom)
        {
            bIsFrom3366 = true;
        }
        else
        {
            return AnsClients(ERROR_VIP_LOGIN_FROM);
        }
    }
    else
    {
        if (TENCENT_LOGIN_FROM_QZONE == stPlayerOnlineData._ucTencentLoginFrom
            || TENCENT_LOGIN_FROM_PENGYOU == stPlayerOnlineData._ucTencentLoginFrom)
        {
            bIsFromYellow = true;
        }
        else if (TENCENT_LOGIN_FROM_QQGAME == stPlayerOnlineData._ucTencentLoginFrom)
        {
            bIsFromBlue = true;
        }
        else if (TENCENT_LOGIN_FROM_3366 == stPlayerOnlineData._ucTencentLoginFrom)
        {
            bIsFrom3366 = true;
        }
    }

    if (stPlayerOnlineData._bIsTencentLogin)
    {
        if (!bHasGetVipInfo)
        {
            bHasGetVipInfo = true;

            //平台校验登录
            CCmdApi* pstCmd = (CCmdApi*)CT_CMDFACTORY->CreateCmd(ID_MAINAPI_TAPI);
            if (!pstCmd)
            {
                CT_WARNING(("create cmd error"));
                return AnsClients(RET_SYS_ERROR);
            }

            pstCmd->pstMainCtrl = CMainCtrl::m_pstMain;
            pstCmd->stReq._ucReqType = CReqApi::API_TENCENT;
            pstCmd->stReq._szTApiName.Set("/v3/user/total_vip_info");
            pstCmd->stReq._stTApiKeyValue = stPlayerOnlineData._stTencentApiKeyValue;

            if (bIsFromYellow)
            {
                pstCmd->stReq._stTApiKeyValue.SetValue("yellow_vip", "1");
                bIsGettingYellowInfo = true;
            }

            if (bIsFromBlue)
            {
                pstCmd->stReq._stTApiKeyValue.SetValue("blue_vip", "1");
                bIsGettingBlueInfo = true;
            }

            if (bIsFrom3366)
            {
                if (!bHasGetBlueInfo)
                {
                    pstCmd->stReq._stTApiKeyValue.SetValue("blue_vip", "1");
                    bIsGettingBlueInfo = true;
                }
                else if (!bHasGet3366Info)
                {
                    pstCmd->stReq._szTApiName.Set("/v3/user/get_info");
                    bIsGetting3366Info = true;
                }
            }

            RegisterChild(pstCmd, true);
            return pstCmd->Do(NULL);
        }

        if (m_pstChild && ID_MAINAPI_TAPI == m_pstChild->GetCmdID())
        {
            CCmdApi* pstCmd = (CCmdApi*)m_pstChild;

            if (bIsGettingYellowInfo)
            {
                bIsYellowVip = atoi(pstCmd->stAns._stTApiKeyValue.GetValue("is_yellow")) == 1;
                ucYellowLevel = (unsigned char)atoi(pstCmd->stAns._stTApiKeyValue.GetValue("yellow_level"));
                bIsYearYellow = atoi(pstCmd->stAns._stTApiKeyValue.GetValue("is_year_yellow")) == 1;

                if (bIsYellowVip)
                {
                    //平台VIP用户缺省送VIP1级
                    pstMainCtrl->m_stVipMgr.SetYellowDefaultVip(stPlayerBaseInfo);
                }

                stAns.set_is_vip(bIsYellowVip ? 1 : 0);
                stAns.set_vip_level(ucYellowLevel);
                stAns.set_is_year_vip(bIsYearYellow ? 1 : 0);

                bIsGettingYellowInfo = false;
                bHasGetYellowInfo = true;
            }
            else if (bIsGettingBlueInfo)
            {
                bIsBlueVip = atoi(pstCmd->stAns._stTApiKeyValue.GetValue("is_blue")) == 1;
                ucBlueLevel = (unsigned char)atoi(pstCmd->stAns._stTApiKeyValue.GetValue("blue_level"));
                bIsYearBlue = atoi(pstCmd->stAns._stTApiKeyValue.GetValue("is_year_blue")) == 1;
                bIsSuperBlue = atoi(pstCmd->stAns._stTApiKeyValue.GetValue("is_high_blue")) == 1;

                if (bIsBlueVip)
                {
                    pstMainCtrl->m_stVipMgr.SetYellowDefaultVip(stPlayerBaseInfo);
                }

                stAns.set_is_vip(bIsBlueVip ? 1 : 0);
                stAns.set_vip_level(ucBlueLevel);
                stAns.set_is_year_vip(bIsYearBlue ? 1 : 0);
                stAns.set_is_super_blue_vip(bIsSuperBlue ? 1 : 0);

                bIsGettingBlueInfo = false;
                bHasGetBlueInfo = true;

                if (bIsFrom3366)
                {
                    bHasGetVipInfo = false;
                    return LogicDo(RET_OK);
                }
            }
            else if (bIsGetting3366Info)
            {
                uc3366Level = (unsigned char)atoi(pstCmd->stAns._stTApiKeyValue.GetValue("3366_grow_level"));

                stAns.set_p3366_level(uc3366Level);

                bIsGetting3366Info = false;
                bHasGet3366Info = true;
            }
        }
    }
    else
    {
#ifdef _DEBUG
        bIsYellowVip = true;
        ucYellowLevel = 1;
        bIsYearYellow = true;

        bIsBlueVip = true;
        ucBlueLevel = 1;
        bIsYearBlue = true;
        bIsSuperBlue = true;

        uc3366Level = 30;

        stAns.set_is_vip(1);
        stAns.set_vip_level(1);
        stAns.set_is_year_vip(1);
        stAns.set_is_super_blue_vip(1);
        stAns.set_p3366_level(uc3366Level);
#endif
    }

    unsigned char ucOpt = stReq.opt();
    stAns.set_opt(ucOpt);

    bool bSaveBaseInfo = false;

    switch (ucOpt)
    {
    case 1: //查询
        {
            /*
            stAns.set_has_get_day_gift(stYellowGiftData._stHasGetDayGift.Get(pstMainCtrl->m_iNow));
            stAns.set_has_get_year_day_gift(stYellowGiftData._stHasGetYearDayGift.Get(pstMainCtrl->m_iNow));
            stAns.set_has_get_newbie_gift(stYellowGiftData._ucHasGetNewbieGift);

            for (int i = 10; i <= stPlayerBaseInfo._shLevel; i += 10)
            {
            stAns.add_has_get_level_gift(stYellowGiftData.HasGetLevelGift(i) ? 1 : 0);
            }

            stAns.set_yellow_vip_level(ucYellowLevel);
            stAns.set_is_year_vip(bIsYearYellow ? 1 : 0);
            */
        }
        break;
    case 2: //打开每日礼包
        {
            if (bIsFromYellow)
            {
                if (!bIsYellowVip)
                {
                    return AnsClients(ERROR_YELLOW_NOT_VIP);
                }

                if (stYellowGiftData._stHasGetDayGift.Get(pstMainCtrl->m_iNow, 0) != 0)
                {
                    return AnsClients(ERROR_YELLOW_HAS_GET_GIFT);
                }

                int iGiftID = 0;
                iGiftID = pstMainCtrl->m_stVipMgr._stYellowGiftConfig.GetDayGiftID(ucYellowLevel);

                if (iGiftID == 0)
                {
                    return AnsClients(ERROR_YELLOW_NO_GIFT);
                }

                //发放奖励
                CDropItem stDropItem;
                iRet = pstMainCtrl->m_stStorageMgr.OpenGift(stPlayerOnlineData._stUserStorage, iGiftID, uiGID, stDropItem);
                if (iRet)
                {
                    return AnsClients(iRet);
                }

                stDropItem.GetPBMsg(uiGID, *(stAns.mutable_drop()));

                //设置已领取
                stYellowGiftData._stHasGetDayGift.Add(1, pstMainCtrl->m_iNow, 0);

                bSaveBaseInfo = true;
            }
            else if (bIsFromBlue || bIsFrom3366)
            {
                if (!bIsBlueVip)
                {
                    return AnsClients(ERROR_BLUE_NOT_VIP);
                }

                if (stBlueGiftData._stHasGetDayGift.Get(pstMainCtrl->m_iNow, 0) != 0)
                {
                    return AnsClients(ERROR_YELLOW_HAS_GET_GIFT);
                }

                int iGiftID = 0;
                iGiftID = pstMainCtrl->m_stVipMgr._stBlueGiftConfig.GetDayGiftID(ucBlueLevel);

                if (iGiftID == 0)
                {
                    return AnsClients(ERROR_YELLOW_NO_GIFT);
                }

                //发放奖励
                CDropItem stDropItem;
                iRet = pstMainCtrl->m_stStorageMgr.OpenGift(stPlayerOnlineData._stUserStorage, iGiftID, uiGID, stDropItem);
                if (iRet)
                {
                    return AnsClients(iRet);
                }

                stDropItem.GetPBMsg(uiGID, *(stAns.mutable_drop()));

                //设置已领取
                stBlueGiftData._stHasGetDayGift.Add(1, pstMainCtrl->m_iNow, 0);

                bSaveBaseInfo = true;
            }
        }
        break;
    case 3: //打开年费礼包
        {
            if (bIsFromYellow)
            {
                if (!bIsYearYellow)
                {
                    return AnsClients(ERROR_VIP_NOT_YEAR_YELLOW);
                }

                if (stYellowGiftData._stHasGetYearDayGift.Get(pstMainCtrl->m_iNow, 0) != 0)
                {
                    return AnsClients(ERROR_YELLOW_HAS_GET_GIFT);
                }

                int iGiftID = 0;
                iGiftID = pstMainCtrl->m_stVipMgr._stYellowGiftConfig.GetYearDayGiftID(ucYellowLevel);

                if (iGiftID == 0)
                {
                    return AnsClients(ERROR_YELLOW_NO_GIFT);
                }

                //发放奖励
                CDropItem stDropItem;
                iRet = pstMainCtrl->m_stStorageMgr.OpenGift(stPlayerOnlineData._stUserStorage, iGiftID, uiGID, stDropItem);
                if (iRet)
                {
                    return AnsClients(iRet);
                }

                stDropItem.GetPBMsg(uiGID, *(stAns.mutable_drop()));

                //设置已领取
                stYellowGiftData._stHasGetYearDayGift.Add(1, pstMainCtrl->m_iNow, 0);

                bSaveBaseInfo = true;
            }
            else if (bIsFromBlue || bIsFrom3366)
            {
                if (!bIsYearBlue)
                {
                    return AnsClients(ERROR_VIP_NOT_YEAR_BLUE);
                }

                if (stBlueGiftData._stHasGetYearDayGift.Get(pstMainCtrl->m_iNow, 0) != 0)
                {
                    return AnsClients(ERROR_YELLOW_HAS_GET_GIFT);
                }

                int iGiftID = 0;
                iGiftID = pstMainCtrl->m_stVipMgr._stBlueGiftConfig.GetYearDayGiftID(ucBlueLevel);

                if (iGiftID == 0)
                {
                    return AnsClients(ERROR_YELLOW_NO_GIFT);
                }

                //发放奖励
                CDropItem stDropItem;
                iRet = pstMainCtrl->m_stStorageMgr.OpenGift(stPlayerOnlineData._stUserStorage, iGiftID, uiGID, stDropItem);
                if (iRet)
                {
                    return AnsClients(iRet);
                }

                stDropItem.GetPBMsg(uiGID, *(stAns.mutable_drop()));

                //设置已领取
                stBlueGiftData._stHasGetYearDayGift.Add(1, pstMainCtrl->m_iNow, 0);

                bSaveBaseInfo = true;
            }
            else
            {
                return AnsClients(RET_SYS_ERROR);
            }
        }
        break;
    case 4: //打开等级礼包
        {
            if (bIsFromYellow)
            {
                if (!bIsYellowVip)
                {
                    return AnsClients(ERROR_YELLOW_NOT_VIP);
                }

                unsigned char ucPos = stReq.pos();
                if (ucPos == 0)
                {
                    return AnsClients(RET_SYS_ERROR);
                }

                short shMinLevel = ucPos * 10;
                if (stYellowGiftData.HasGetLevelGift(shMinLevel))
                {
                    return AnsClients(ERROR_YELLOW_HAS_GET_GIFT);
                }

                if (stPlayerBaseInfo._shLevel < shMinLevel)
                {
                    return AnsClients(ERROR_YELLOW_LOW_LEVEL);
                }

                int iGiftID = pstMainCtrl->m_stVipMgr._stYellowGiftConfig.GetLevelGiftID(shMinLevel);
                if (iGiftID == 0)
                {
                    return AnsClients(ERROR_YELLOW_NO_GIFT);
                }

                //发放奖励
                CDropItem stDropItem;
                iRet = pstMainCtrl->m_stStorageMgr.OpenGift(stPlayerOnlineData._stUserStorage, iGiftID, uiGID, stDropItem);
                if (iRet)
                {
                    return AnsClients(iRet);
                }

                stDropItem.GetPBMsg(uiGID, *(stAns.mutable_drop()));

                //设置已领取
                stYellowGiftData.SetGetLevelGift(shMinLevel);

                bSaveBaseInfo = true;
            }
            else if (bIsFromBlue || bIsFrom3366)
            {
                if (!bIsBlueVip)
                {
                    return AnsClients(ERROR_BLUE_NOT_VIP);
                }

                unsigned char ucPos = stReq.pos();
                if (ucPos == 0)
                {
                    return AnsClients(RET_SYS_ERROR);
                }

                short shMinLevel = ucPos * 10;
                if (stBlueGiftData.HasGetLevelGift(shMinLevel))
                {
                    return AnsClients(ERROR_YELLOW_HAS_GET_GIFT);
                }

                if (stPlayerBaseInfo._shLevel < shMinLevel)
                {
                    return AnsClients(ERROR_YELLOW_LOW_LEVEL);
                }

                int iGiftID = pstMainCtrl->m_stVipMgr._stBlueGiftConfig.GetLevelGiftID(shMinLevel);
                if (iGiftID == 0)
                {
                    return AnsClients(ERROR_YELLOW_NO_GIFT);
                }

                //发放奖励
                CDropItem stDropItem;
                iRet = pstMainCtrl->m_stStorageMgr.OpenGift(stPlayerOnlineData._stUserStorage, iGiftID, uiGID, stDropItem);
                if (iRet)
                {
                    return AnsClients(iRet);
                }

                stDropItem.GetPBMsg(uiGID, *(stAns.mutable_drop()));

                //设置已领取
                stBlueGiftData.SetGetLevelGift(shMinLevel);

                bSaveBaseInfo = true;
            }
        }
        break;
    case 5: //打开新手礼包
        {
            if (bIsFromYellow)
            {
                if (!bIsYellowVip)
                {
                    return AnsClients(ERROR_YELLOW_NOT_VIP);
                }

                if (stYellowGiftData._ucHasGetNewbieGift != 0)
                {
                    return AnsClients(ERROR_YELLOW_HAS_GET_GIFT);
                }

                int iGiftID = pstMainCtrl->m_stVipMgr._stYellowGiftConfig.GetNewbieGiftID();
                if (iGiftID == 0)
                {
                    return AnsClients(ERROR_YELLOW_NO_GIFT);
                }

                //发放奖励
                CDropItem stDropItem;
                iRet = pstMainCtrl->m_stStorageMgr.OpenGift(stPlayerOnlineData._stUserStorage, iGiftID, uiGID, stDropItem);
                if (iRet)
                {
                    return AnsClients(iRet);
                }

                stDropItem.GetPBMsg(uiGID, *(stAns.mutable_drop()));

                //设置已领取
                stYellowGiftData._ucHasGetNewbieGift = 1;

                bSaveBaseInfo = true;
            }
            else if (bIsFromBlue || bIsFrom3366)
            {
                if (!bIsBlueVip)
                {
                    return AnsClients(ERROR_BLUE_NOT_VIP);
                }

                if (stBlueGiftData._ucHasGetNewbieGift != 0)
                {
                    return AnsClients(ERROR_YELLOW_HAS_GET_GIFT);
                }

                int iGiftID = pstMainCtrl->m_stVipMgr._stBlueGiftConfig.GetNewbieGiftID();
                if (iGiftID == 0)
                {
                    return AnsClients(ERROR_YELLOW_NO_GIFT);
                }

                //发放奖励
                CDropItem stDropItem;
                iRet = pstMainCtrl->m_stStorageMgr.OpenGift(stPlayerOnlineData._stUserStorage, iGiftID, uiGID, stDropItem);
                if (iRet)
                {
                    return AnsClients(iRet);
                }

                stDropItem.GetPBMsg(uiGID, *(stAns.mutable_drop()));

                //设置已领取
                stBlueGiftData._ucHasGetNewbieGift = 1;

                bSaveBaseInfo = true;
            }
        }
        break;
    case 6: //打开蓝钻豪华礼包
        {
            if (bIsFromBlue || bIsFrom3366)
            {
                if (!bIsSuperBlue)
                {
                    return AnsClients(ERROR_VIP_NOT_SUPER_BLUE);
                }

                if (stBlueGiftData._stHasGetSuperDayGift.Get(pstMainCtrl->m_iNow, 0) != 0)
                {
                    return AnsClients(ERROR_YELLOW_HAS_GET_GIFT);
                }

                int iGiftID = 0;
                iGiftID = pstMainCtrl->m_stVipMgr._stBlueGiftConfig._iSuperVipGiftID;
                if (iGiftID == 0)
                {
                    return AnsClients(ERROR_YELLOW_NO_GIFT);
                }

                //发放奖励
                CDropItem stDropItem;
                iRet = pstMainCtrl->m_stStorageMgr.OpenGift(stPlayerOnlineData._stUserStorage, iGiftID, uiGID, stDropItem);
                if (iRet)
                {
                    return AnsClients(iRet);
                }

                stDropItem.GetPBMsg(uiGID, *(stAns.mutable_drop()));

                //设置已领取
                stBlueGiftData._stHasGetSuperDayGift.Add(1, pstMainCtrl->m_iNow, 0);

                bSaveBaseInfo = true;
            }
            else
            {
                return AnsClients(RET_SYS_ERROR);
            }
        }
        break;

    case 7:  //3366每日礼包
        {
            if (bIsFrom3366)
            {
                if (stBlueGiftData._stHasGet3366DayGift.Get(pstMainCtrl->m_iNow, 0) != 0)
                {
                    return AnsClients(ERROR_YELLOW_HAS_GET_GIFT);
                }

                int iGiftID = 0;
                iGiftID = pstMainCtrl->m_stVipMgr._stBlueGiftConfig.Get3366DayGiftID(uc3366Level);

                if (iGiftID == 0)
                {
                    return AnsClients(ERROR_YELLOW_NO_GIFT);
                }

                //发放奖励
                CDropItem stDropItem;
                iRet = pstMainCtrl->m_stStorageMgr.OpenGift(stPlayerOnlineData._stUserStorage, iGiftID, uiGID, stDropItem);
                if (iRet)
                {
                    return AnsClients(iRet);
                }

                stDropItem.GetPBMsg(uiGID, *(stAns.mutable_drop()));

                //设置已领取
                stBlueGiftData._stHasGet3366DayGift.Add(1, pstMainCtrl->m_iNow, 0);

                bSaveBaseInfo = true;
            }
            else
            {
                return AnsClients(RET_SYS_ERROR);
            }
        }
        break;

    case 8:  //领取坐骑
        {
            if (bIsBlueVip || bIsYellowVip)
            {
                bool bHasGetMount = ((true == stBlueGiftData._ucHasGetMount) || (true == stYellowGiftData._ucHasGetMount));  //黄钻或者蓝钻只能一次
                if (bHasGetMount)
                {
                    return AnsClients(ERROR_VIP_MOUNT_REP);
                }

                if (stPlayerBaseInfo._shLevel < 41)
                {
                    return AnsClients(ERROR_VIP_MOUNT_LEVEL);
                }

                CDropItem stDropItem;
                iRet = pstMainCtrl->m_stVipMgr.GetVipMount(bIsYellowVip, stDropItem);
                if (iRet)
                {
                    return AnsClients(iRet);
                }

                iRet = pstMainCtrl->m_stItemMgr.DropItem (0, uiGID, stDropItem, true);
                if (iRet)
                {
                    return AnsClients(iRet);
                }

                if (bIsBlueVip)
                {
                    stBlueGiftData._ucHasGetMount = true;
                }

                if (bIsYellowVip)
                {
                    stYellowGiftData._ucHasGetMount = true;
                }
            }
            else
            {
                return AnsClients(ERROR_YELLOW_NOT_VIP);
            }
        }
        break;

    default:
        return  AnsClients(RET_SYS_ERROR);
    }

    if (bIsFromYellow && bIsYellowVip)
    {
        stAns.set_has_get_day_gift(stYellowGiftData._stHasGetDayGift.Get(pstMainCtrl->m_iNow, 0));
        stAns.set_has_get_year_day_gift(stYellowGiftData._stHasGetYearDayGift.Get(pstMainCtrl->m_iNow, 0));
        stAns.set_has_get_newbie_gift(stYellowGiftData._ucHasGetNewbieGift);

        for (int i = 10; i <= stPlayerBaseInfo._shLevel; i += 10)
        {
            stAns.add_has_get_level_gift(stYellowGiftData.HasGetLevelGift(i) ? 1 : 0);
        }
    }

    stAns.set_has_get_mount(stYellowGiftData._ucHasGetMount || stBlueGiftData._ucHasGetMount);
    if (bIsFromYellow)
    {
        stAns.set_mount_id(pstMainCtrl->m_stVipMgr._stYellowGiftConfig._usYellowMount);
    }

    if ((bIsFromBlue || bIsFrom3366) && bIsBlueVip)
    {
        stAns.set_has_get_day_gift(stBlueGiftData._stHasGetDayGift.Get(pstMainCtrl->m_iNow, 0));
        stAns.set_has_get_year_day_gift(stBlueGiftData._stHasGetYearDayGift.Get(pstMainCtrl->m_iNow, 0));
        stAns.set_has_get_newbie_gift(stBlueGiftData._ucHasGetNewbieGift);
        stAns.set_has_get_super_blue_gift(stBlueGiftData._stHasGetSuperDayGift.Get(pstMainCtrl->m_iNow, 0));

        for (int i = 10; i <= stPlayerBaseInfo._shLevel; i += 10)
        {
            stAns.add_has_get_level_gift(stBlueGiftData.HasGetLevelGift(i) ? 1 : 0);
        }  
    }
    
    if (bIsFromBlue)
    {
        stAns.set_mount_id(pstMainCtrl->m_stVipMgr._stBlueGiftConfig._usBlueMount);
    }

    if (bIsFrom3366)
    {
        stAns.set_has_get_3366_day_gift(stBlueGiftData._stHasGet3366DayGift.Get(pstMainCtrl->m_iNow, 0));
    }

    if (bSaveBaseInfo)
    {
        //保存数据 base info
        CVarObjectKey stKey;
        stKey.m_chDataType = DT_PLAYER_BASE_INFO;
        stKey.m_u64ID = uiGID;
        pstMainCtrl->UpdateData(stKey, stPlayerBaseInfo, VOP_UPDATE);
    }

    return AnsClients(iRet);
}

int CCmdYellowGift::AnsClients(int iRet)
{
    pstMainCtrl->m_stEncodeBuf.Clear();

    stMsgHead.set_msgtype(CSMsgDef::EMT_ANSWER);
    stMsgHead.set_msgret(iRet);

    if (iRet != RET_OK)
    {
        pstMainCtrl->ReturnMsgHead(stMsgHead, stEnpHead);
        return Done(iRet);
    }

    pstMainCtrl->EncodePBMsg(stMsgHead, pstMainCtrl->m_stEncodeBuf);
    pstMainCtrl->EncodePBMsg(stAns, pstMainCtrl->m_stEncodeBuf);

    if (pstMainCtrl->AnswerToClient(stEnpHead, pstMainCtrl->m_stEncodeBuf, true))
    {
        return Done(RET_SYS_ERROR);
    }

    return Done(iRet);
}
